package com.elong.android.specialhouse.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.entity.AdImageInfo;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.List;

/**
 * 
 * @ClassName: HomeAdsGalleryAdapter
 * @Description: 首页广告条
 * @author: hongjun guo
 * @date:2014-1-9 上午10:44:04
 */
public class HomeAdsGalleryAdapter extends BaseAdapter {

	protected ImageLoader mImageLoader = ImageLoader.getInstance();
	private DisplayImageOptions mOptions;

	private List<AdImageInfo> mAdsListForAdapter;

	public HomeAdsGalleryAdapter(List<AdImageInfo> list) {
		this.mAdsListForAdapter = list;
		
		mOptions = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.bg_loading_default)
				.showStubImage(R.drawable.bg_loading_default).imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
				.cacheInMemory().cacheOnDisc().build();
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int position) {
		if (mAdsListForAdapter == null || mAdsListForAdapter.size() < 1) {
			return null;
		}
		position = position % mAdsListForAdapter.size();
		return mAdsListForAdapter.get(position);
	}

	@Override
	public long getItemId(int position) {
		if (mAdsListForAdapter == null || mAdsListForAdapter.size() < 1) {
			return 0;
		}
		return position % mAdsListForAdapter.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_ads, null);
			holder = new ViewHolder();
			holder.imageView = (ImageView) convertView.findViewById(R.id.image);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		AdImageInfo ads = mAdsListForAdapter.get(position % mAdsListForAdapter.size());

		mImageLoader.displayImage(ads.imageUrl, holder.imageView, mOptions);
		return convertView;
	}

	private class ViewHolder {
		public ImageView imageView;
	}
}
